﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace Yugi.Client.ViewModel
{
    public class ChatManagerModel
    {
        public ObservableCollection<ChatModel> ListChat;
        MainServerHandler main;
        public ChannelModel Channel_Model;
        public Action<string, string> Added;
        public ChatManagerModel(MainServerHandler m)
        {
            main = m;
            ListChat = new ObservableCollection<ChatModel>();
            Channel_Model = new ChannelModel(main);
        }
        public int Add(string s1, string s2)
        {
            ChatModel chat = new ChatModel(main, s1, s2);
            foreach (ChatModel c in ListChat)
            {
                if (s2 == c.to)
                {
                    return 0;
                }
            }
            ListChat.Add(chat);
            if (Added != null)
                Added(s1, s2);
            return 1;
        }

        public int Remove(string s)
        {
            foreach (ChatModel c in ListChat)
            {
                if (s == c.to)
                {
                    ListChat.Remove(c);
                    return 1;
                }
            }
            return 0;
        }

        public void ReceiveMSG(ClientMSG msg, params object[] obj)
        {
            if (msg == ClientMSG.Chat_Channel)
            {
                Channel_Model.ReceiveMSG(msg, obj);
                return;
            }
            int i;
            for (i = 0; i < ListChat.Count; i++)
            {
                if (ListChat[i].to == (string)obj[0])
                {
                    ListChat[i].Presenter.Do(msg, obj);
                    return;
                }
            }
            if (i == ListChat.Count)
            {
                Add((string)obj[1], (string)obj[0]);
                ListChat[ListChat.Count - 1].Presenter.Do(msg, obj);
            }
        }
    }

}
